package com.jvm.d7;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.nio.file.Path;
import java.nio.file.PathMatcher;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Matcher;

/**
 * 描述:
 *
 * @author luzhongfu
 * Created by lucky on 2021-10-09 2:08 PM
 */
public class PathMatcherProvider {

    private static MethodHandles.Lookup lookup = MethodHandles.lookup();

    private static MethodType methodType = MethodType.methodType(void.class,String.class);

    private static Map<String , MethodHandle> handleMap = new HashMap<>();

    public PathMatcher getPathMather(String name, String arg) throws Throwable {

        Objects.requireNonNull(name);
        Objects.requireNonNull(arg);

        MethodHandle methodHandle = handleMap.get(name);

        return (PathMatcher)methodHandle.invokeExact(arg);
    }

    public PathMatcherProvider(){
        try {
            MethodHandle constructor = lookup.findConstructor(GlobPathMatcher.class, methodType);
            MethodType methodType = constructor.type().changeReturnType(PathMatcher.class);
            handleMap.put("glob",constructor.asType(methodType));
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws Throwable {
        PathMatcherProvider pathMatcherProvider = new PathMatcherProvider();
        PathMatcher glob = pathMatcherProvider.getPathMather("glob", "*a");

        System.out.println(glob);
    }

    public static class GlobPathMatcher implements PathMatcher{

        public GlobPathMatcher(String args){

            System.out.println(args);
        }

        @Override
        public boolean matches(Path path) {
            return false;
        }
    }


}
